import { Component, EventEmitter, OnDestroy, OnInit, Output } from '@angular/core';
import { ApiService } from '@core/services/http/api.service';
import { Subscription } from 'rxjs';
import { AssemblyDrawingViewService } from '@core/services/assemblyDrawing/assemblyDrawing.view.service';
import { AssemblyDrawingService } from '@core/services/assemblyDrawing/assemblyDrawing.service';

/***
 * 目录清单
 */

@Component({
  selector: 'cad-menu',
  templateUrl: './cad-menu.component.html',
  styleUrls: ['./cad-menu.component.scss'],
})
export class CadMenuComponent implements OnInit, OnDestroy {

  @Output() selectedMenu = new EventEmitter();
  data = [];
  sourceData: any = []; // 原始数据，记录类型
  dataSource: 'CAD' | 'OWNER' = null; //展示类型

  isLoading: boolean = false;

  subscription: Subscription;

  constructor(
    private apiService: ApiService,
    private assemblyDrawingViewService: AssemblyDrawingViewService,
    private assemblyDrawingService: AssemblyDrawingService,
  ) {
  }

  ngOnInit(): void {
    this.subscription = this.assemblyDrawingViewService.menuChange$.subscribe((cataLog: any) => {
      this.isLoading = false;
      if (cataLog && cataLog.length != 0) {
        this.dataSource = 'CAD';
        this.data = cataLog;
        this.sourceData = cataLog;
      } else {
        // this.dataSource = null;
        this.dataSource = 'CAD';
        this.data = [];
        this.sourceData = [];
      }
    });
    this.assemblyDrawingViewService.subgroupChange$.subscribe(() => this.isLoading = false);
  }

  ngOnDestroy(): void {
    this.subscription.unsubscribe();
  }

  onSelect(node) {
    // console.log('当前选中的主组数据', node);
    this.isLoading = true;
    this.assemblyDrawingService.selectMenu$.next({ ...node, dataSource: 'CAD' });
  }

  switchMenuModel(dataSource) {
    // console.log('当前选中菜单',dataSource);
    // if (dataSource === 'OWNER' && this.dataSource === 'OWNER') return;
    if (dataSource === 'CAD' && this.dataSource === 'CAD') return;
    // this.assemblyDrawingService.switchMenuModel({ dataSource });
  }

}
